package com.zhcd.utils;

import com.alibaba.fastjson.JSON;
import lombok.Data;

@Data
public class JsonResult {
    public String Code;//状态码
    public boolean OK;//是否成功
    public int Total;//数据条数
    public Object Data;//数据集
    public String message;//提示信息

    public String IsError(String msg){
        setCode("400");
        setOK(false);
        setTotal(0);
        setMessage(msg);
        return ToJson();
    }
    public String IsOK(String msg){
        setCode("200");
        setOK(true);
        setTotal(0);
        setMessage(msg);
        return ToJson();
    }

    public String IsOK(Object data){
        setCode("200");
        setOK(true);
        setTotal(1);
        setData(data);
        return ToJson();
    }
    public String IsOK(Integer total,Object data){
        setCode("200");
        setOK(true);
        setTotal(total);
        setData(data);
        return ToJson();
    }
    public String ToJson(){
        var result="{\"Code\":\""+Code+"\""
                +",\"IsOK\":"+OK
                +",\"Total\":"+Total
                +",\"Data\":"+ JSON.toJSON(Data)
                +",\"message\":\""+message+"\"}";
        return  result;
    }
}
